package com.binhthuan.catechist.server.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.binhthuan.catechist.server.ResponseObject;
import com.binhthuan.catechist.server.bus.DateBUS;
import com.binhthuan.catechist.server.bus.CourseBUS;
import com.binhthuan.catechist.server.bus.ScheduleBUS;
import com.binhthuan.catechist.server.domain.Date;
import com.binhthuan.catechist.server.domain.Course;
import com.binhthuan.catechist.server.domain.Schedule;
import com.binhthuan.catechist.server.domain.ScheduleStatus;
import com.binhthuan.catechist.shared.constant.Parameter;

public class ScheduleServlet extends AbstractServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -5911527668977791235L;

    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        addSchedule(request, response);
    }

    @Override
    protected void fetch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        getCourseSchedule(request, response);
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response, boolean confirmation) throws ServletException,
            IOException {
        deleteSchedule(request, response);
    }

    @Override
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        updateSchedule(request, response);
    }

    private void getCourseSchedule(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ScheduleBUS bus = new ScheduleBUS();
        String doctrineClassId = request.getParameter(Parameter.COURSE);
        try {
            int classId = Integer.parseInt(doctrineClassId);
            List<Schedule> schedules = bus.getScheduleByClass(classId);
            sendResponse(new ResponseObject(schedules).toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void addSchedule(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String doctrineClassId = request.getParameter(Parameter.COURSE);
        String dateId = request.getParameter(Parameter.DATE);
        String statusId = request.getParameter(Parameter.STATUS);
        String title = request.getParameter(Parameter.TITLE);
        String content = request.getParameter(Parameter.CONTENT);
        String comment = request.getParameter(Parameter.COMMENT);

        try {
            int classId = Integer.parseInt(doctrineClassId);
            int scheduleDayId = Integer.parseInt(dateId);
            int scheduleStatusId = Integer.parseInt(statusId);

            CourseBUS doctrineClassBus = new CourseBUS();
            Course doctrineClass = doctrineClassBus.getCourseById(classId);

            DateBUS dateBus = new DateBUS();
            Date scheduleDay = dateBus.getSchoolDateById(scheduleDayId);

            ScheduleBUS scheduleBus = new ScheduleBUS();
            ScheduleStatus scheduleStatus = scheduleBus.getScheduleStatusById(scheduleStatusId);

            Schedule schedule = new Schedule();
            schedule.setTitle(title);
            schedule.setContent(content);
            schedule.setComment(comment);
            schedule.setCourse(doctrineClass);
            schedule.setDate(scheduleDay);
            schedule.setStatus(scheduleStatus);

            if (scheduleBus.save(schedule)) {
                sendResponse(new ResponseObject(schedule).toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void updateSchedule(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String scheduleId = request.getParameter(Parameter.ID);
        String doctrineClassId = request.getParameter(Parameter.COURSE);
        String dayIdString = request.getParameter(Parameter.DATE);
        String statusIdString = request.getParameter(Parameter.STATUS);
        String title = request.getParameter(Parameter.TITLE);
        String content = request.getParameter(Parameter.CONTENT);
        String comment = request.getParameter(Parameter.COMMENT);

        try {
            int id = Integer.parseInt(scheduleId);
            int classId = Integer.parseInt(doctrineClassId);
            int dayId = Integer.parseInt(dayIdString);
            int statusId = Integer.parseInt(statusIdString);
            
            Course doctrineClass = new Course();
            doctrineClass.setId(classId);

            Date day = new Date();
            day.setId(dayId);

            ScheduleStatus status = new ScheduleStatus();
            status.setId(statusId);

            ScheduleBUS scheduleBus = new ScheduleBUS();

            Schedule schedule = new Schedule();
            schedule.setId(id);
            schedule.setTitle(title);
            schedule.setContent(content);
            schedule.setComment(comment);
            schedule.setCourse(doctrineClass);
            schedule.setDate(day);
            schedule.setStatus(status);

            if (scheduleBus.save(schedule)) {
                schedule = scheduleBus.getScheduleById(id);
                sendResponse(new ResponseObject(schedule).toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void deleteSchedule(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String scheduleId = request.getParameter(Parameter.ID);

        try {
            int id = Integer.parseInt(scheduleId);

            ScheduleBUS scheduleBus = new ScheduleBUS();
            if (scheduleBus.deleteSchedule(id)) {
                sendResponse(new ResponseObject((Object) null).toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }
}
